import { JSIModuleSpec } from './JSIModuleSpec';
import { JSIModule } from './JSIModule';

export class JSIModuleHolder {

  private mModule: JSIModule;
  private readonly mSpec: JSIModuleSpec<JSIModule>;

  public constructor(spec: JSIModuleSpec<JSIModule>) {
    this.mSpec = spec;
  }

  public getJSIModule(): JSIModule {
    if (this.mModule == null) {
      //synchronized (this) {
        if (this.mModule != null) {
          return this.mModule;
        }
        this.mModule = this.mSpec.getJSIModuleProvider().get();
        this.mModule.initialize();
     //}
    }
    return this.mModule;
  }

  public notifyJSInstanceDestroy() {
    if (this.mModule != null) {
      this.mModule.onCatalystInstanceDestroy();
    }
  }
}
